package org.okkam.webid.model;

import java.io.InputStream;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.util.FileManager;

public class VerifyWebID {
	Model model;
	public boolean isAllowed(String webID, String fileName){
		boolean allowed = false;
		//ServletContext sc = getServletContext();
		InputStream in = FileManager.get().open(fileName);
		if (in == null) {		    
			System.out.println("File Not Found");
		}
		else {
		// Create the input model. Models different from the default one import also the 
		// rdf and rdf-schema axioms. The input model usually comes with blank nodes. 
			model = ModelFactory.createDefaultModel();	
		// read the RDF/TURTLE file
			model.read(in, null, "TURTLE");
			
		}
		if(model != null){
			Resource sub = model.createResource(webID);
			Property prop = model.createProperty("http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
			RDFNode pred = model.createResource("http://xmlns.com/foaf/0.1#Person");
			StmtIterator it = model.listStatements(sub, prop , pred) ;
/*			String prefix = "PREFIX foaf: <http://xmlns.com/foaf/0.1/> " +
					"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> ";
			String queryString = prefix + '\n'
				+ "ask { " +webID+" ?x foaf:Person. } ";
			
			System.out.println(queryString);
			
			Query query = QueryFactory.create(queryString) ;
			QueryExecution qexec = QueryExecutionFactory.create(query, model) ;
			allowed = qexec.execAsk() ;
			qexec.close() ;*/
			if(it.hasNext()){
				allowed = true;
			}
		}
		
		return allowed;
	}
	/**
	 * authenticate a user tried to loggin with a certificate based on the FOAF+SSL webID method
	 * 
	 * @param webID the webID extracted from the certeficate
	 * @param modulus modulus of the public key
	 * @param exponent exponent of the public key
	 * @param fileName // file name (for test purpose) SPARQL end point of the rdf store
	 * @return
	 */
	public boolean isAuthenticated(String webID, String modulus, String exponent, String fileName ){
	

		System.setProperty("http.proxyHost", "proxy.unitn.it");
		System.setProperty("http.proxyPort", "3128");
		
		boolean allowed = false;
		//ServletContext sc = getServletContext();
		FileManager fm = new FileManager();
		//fm.addLocatorURL();
		InputStream in = fm.get().open(fileName);
		//InputStream in  = FileManager.get().open("http://www.sharesemantics.com/people/luigi/foaf.rdf");
		if (in == null) {		    
			System.out.println("File Not Found");
		}
		else {
			model = ModelFactory.createDefaultModel();	
		// read the RDF/TURTLE file
			model.read(in, null, "RDF/XML");
			//model.write(System.out,"TTL");
			
		}
		if(model != null){
			//Resource sub = model.createResource("http://www.w3.org/ns/auth/cert#key");
			Property prop = model.createProperty("http://www.w3.org/ns/auth/cert#key");
			//RDFNode pred = model.createResource("http://xmlns.com/foaf/0.1/Person");
			StmtIterator it = model.listStatements() ;
			
			Property propexponent = model.createProperty("http://www.w3.org/ns/auth/cert#exponent");
			Property propemodulus = model.createProperty("http://www.w3.org/ns/auth/cert#modulus");
			
			Property propexponent2 = model.createProperty("http://www.w3.org/ns/auth/cert#decimal");
			Property propemodulus2 = model.createProperty("http://www.w3.org/ns/auth/cert#hex");
			
			String modulustemp = null;
			String exponenttemp = null;
			boolean modulusfound = false,exponentfound = false;
			while (it.hasNext()){
				Statement statment = it.next();
				Resource subject = statment.getSubject();
				Property predicate = statment.getPredicate();
				RDFNode object = statment.getObject();
				if(propemodulus.equals(predicate)||propemodulus2.equals(predicate)){
					modulustemp = object.toString();
					modulusfound = true;
					if(exponentfound) break;	
				}
				
				if(propexponent.equals(predicate)||propexponent2.equals(predicate)){
					exponenttemp = object.toString();
					exponentfound = true;
					if(modulusfound) break;	
				}
				
			}
			if(modulustemp.contains("\"")){	
				modulustemp = modulustemp.toString().substring(0,modulustemp.toString().indexOf("\"")).trim();
			}
			
			if( modulustemp.contains("^")){	
				modulustemp = modulustemp.toString().substring(0,modulustemp.toString().indexOf("^")).trim();
			}
			
			if(exponenttemp.contains("\"")){	
				exponenttemp = exponenttemp.toString().substring(0,exponenttemp.toString().indexOf("\"")).trim();
			}
			
			if( exponenttemp.contains("^")){	
				modulustemp = exponenttemp.toString().substring(0,exponenttemp.toString().indexOf("^")).trim();
			}
			
						
			//	exponenttemp = exponenttemp.toString().substring(0,exponenttemp.toString().indexOf("^")).trim();
			
			
			if(modulustemp.equals(modulus.trim())&&exponenttemp.equals(exponent.trim())){
				allowed = true;
			}
			
			
		}
		
		return allowed;
	}
}
